package com.easyjava.vuebuilder;

import com.easyjava.builder.BuildBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author: 小闹心
 * @Description: 定义常量类
 * @DateTime: 2024/11/21 10:49
 **/
public class BuilderVueCommon {
    public static final Logger logger = LoggerFactory.getLogger(BuilderVueCommon.class);
    public static void execute(String fileName, String outPutPath, List<String> contentList){
        File folder = new File(outPutPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        File javaFile = new File(folder,fileName + ".js");
        OutputStream out = null;
        OutputStreamWriter outWriter = null;
        BufferedWriter bw = null;
        try{
            out = new FileOutputStream(javaFile);
            outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outWriter);
            for(String content : contentList){
                bw.write(content);
                bw.newLine();
            }
            bw.flush();
        }catch (Exception e){
            logger.error("生成常量类: {} 失败" ,fileName);
        }finally {
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outWriter != null){
                try {
                    outWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
